package com.xl.web;

import com.xl.dto.ConfigurationPropertiesTest;
import com.xl.dto.Student;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * Created with 徐立.
 *
 * @author 徐立
 * @date 2019-08-21
 * @time 23:23
 * To change this template use File | Settings | File Templates.
 */
@RestController
@Slf4j
@RequestMapping("/index")
public class IndexAction {
    @Autowired
    private Environment environment;
    @Autowired(required=false)
    private ConfigurationPropertiesTest configurationPropertiesTest;
    private final AtomicInteger num = new AtomicInteger();
    
    //@Autowired(required = false)
    //private XLConfig xlConfig;
    @PostMapping("/post")
    public String getPost(HttpServletRequest request) {
        if (request == null) {
            return "map是空";
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        StringJoiner result = new StringJoiner("\n");
        parameterMap.forEach((s, strings) -> {
            String format = MessageFormat.format("{0}:{1}", s,
                    Arrays.stream(strings).collect(Collectors.joining(",")));
            result.add(format);
        });
        log.info("post请求参数是：{}", result.toString());
        return result.toString();
    }
    
    @GetMapping("/getValitate")
    public Student getValitate(@Valid Student student) {
        log.info(student.toString());
        return student;
    }
    
    @GetMapping("page")
    public String get() {
        return "测试没有加/能否访问";
    }
    
    @GetMapping("/")
    public String index(@RequestParam(required = false) Integer id) {
        log.info(configurationPropertiesTest.getName());
        int o = num.incrementAndGet();
        String messageFormat = MessageFormat.format("第{0}次进入index,id是{1},端口号是{2}", o, id,
                environment.getProperty("local.server.port"));
        log.info(messageFormat);
        return messageFormat;
    }
    
    @GetMapping("/dataConfig")
    public String getDataConfig() {
        log.info("这是/dataConfig方法");
        return "xlConfig.toString()";
    }
}
